package com.dianrui.moonfire.walkbaby;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.activity.BaseActivity;
import com.dianrui.moonfire.event.OpenOrCloseBaby;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.CountDownTimerUtils;
import com.dianrui.moonfire.util.JsonUtils;
import com.dianrui.moonfire.util.MyUtil;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Description:遛娃借车中
 * User: lzy
 * Date: 2020-8-17
 */
public class WalkBabyRidingActivity extends BaseActivity {
    private int mRunTimes;
    private String mOrderId;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.billing_rules)
    TextView billing_rules;
    @BindView(R.id.price_rules)
    TextView priceRules;
    @BindView(R.id.content)
    TextView showContent;

    @Override
    public int getContentViewId() {
        return R.layout.activity_walkbaby_riding;
    }

    @Override
    public void initView() {
        customInit(true);
        title.setText(getString(R.string.walkbabyriding_title));
        getTime();
        getCarInfo();
        pricecountDownTimer.start();
    }

    private void getTime() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        XHttpRequest.getInstance().postRequests(Url.BABYPAYRIDING, jsonObject.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                mRunTimes = jsonObject.optJSONObject("data").optInt("run_times");
                                time.setText(jsonObject.optJSONObject("data").optString("run_time"));
                                price.setText(jsonObject.optJSONObject("data").optString("money"));
                                number.setText(jsonObject.optJSONObject("data").optString("number"));
                                mOrderId=jsonObject.optJSONObject("data").optString("order_id");
                            }else {
                                String data = JsonUtils.getJsonStr(result, "data");
                                EventBus.getDefault().post(new OpenOrCloseBaby(false));
                                Intent intent=new Intent(WalkBabyRidingActivity.this,WalkBabyReturnCarActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putString("order_id",mOrderId);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    public void getCarInfo() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        try {
            XHttpRequest.getInstance().postRequests(Url.BABYPAYRIDING, jsonObject.toString(), new XHttpRequest.XCallBack() {
                @Override
                public void onResponse(String result) {
                    if (!StringUtils.isEmpty(result)) {
                        try {
                            final JSONObject jsonObject = new JSONObject(result);
                            if (null != jsonObject && !isFinishing()) {
                                if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                    String data = JsonUtils.getJsonStr(result, "data");
                                    billing_rules.setText(JsonUtils.getJsonStr(data,"billing_rules"));
                                    priceRules.setText(JsonUtils.getJsonStr(data,"price_rules"));
                                    showContent.setText(JsonUtils.getJsonStr(data,"content"));
                                }else {
                                    String data = JsonUtils.getJsonStr(result, "data");
                                    EventBus.getDefault().post(new OpenOrCloseBaby(false));
                                    Intent intent=new Intent(WalkBabyRidingActivity.this,WalkBabyReturnCarActivity.class);
                                    Bundle bundle=new Bundle();
                                    bundle.putString("order_id",mOrderId);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void failedRequest(String result) {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            });
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }


    //每1秒走定时器
    CountDownTimerUtils pricecountDownTimer = new CountDownTimerUtils(Long.MAX_VALUE, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mRunTimes++;
            String showTime = MyUtil.FormatMiss(mRunTimes);
            time.setText(showTime);
            if (mRunTimes % 3 == 0) {
                getTime();
            }
        }

        @Override
        public void onFinish() {
        }
    };



    @Override
    protected void onRestart() {
        super.onRestart();
        getTime();
        getCarInfo();

    }

    @Override
    public void initData() {}

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (pricecountDownTimer != null) {
            pricecountDownTimer.cancel();
            pricecountDownTimer=null;
        }
    }
}
